// pages/order/order.js
const app = getApp()
const ApiRootUrl = 'http://8.136.195.245:8000/';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    TabCur: 0,
    scrollLeft: 0,

    pickerArray: ['card1'],
    index: 0,
    selectCardUid: "",
    haveNocard: false,
    beforeSearch:true,

    MyCardList: [{
      cardId: 1,
      cardUid: "card1",
      cardMoney: 100,

      isBind: 1,
      gmtCreate: "2021-06-21 14:23:15",
      fruitPicture: 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1070649221,78984387&fm=26&gp=0.jpg'
    }, {
      cardId: 2,
      cardUid: "card2",
      cardMoney: 100,
      isBind: 1,
      gmtCreate: "2021-06-21 14:23:15",
      fruitPicture: 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1070649221,78984387&fm=26&gp=0.jpg'
    }],

    CardChargeList: [{
      cardUid: "card1",
      chargeMoney: 20,
      cardMoney: 100,
      isBind: 1,
      gmtCreate: "2021-06-21 14:23:15",
    }],

    CardOrderList: [{
      cardUid: "card1",
      totalMoney: 20,
      fruitName:"水蜜桃",
      cardMoney: 100,
      isBind: 1,
      gmtCreate: "2021-06-21 14:23:15",
    }],

  },
  onLoad: function () {
    this.getMyCards();
  },

  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value,
      selectCardUid: this.data.pickerArray[e.detail.value],
      beforeSearch:true,
      CardOrderList:[],
      CardChargeList:[]
    })
    console.log("选择的卡片uid为：", this.data.selectCardUid)
    this.getCardChargeRecord()
    this.getCardOrderRecord()
  },



  getMyCards: function () {
    var that1 = this
    wx.request({
      // url: "http://localhost:8001/card/list/" + app.globalData.userId,
      url: ApiRootUrl+app.globalData.userId,
      // url: "http://8.136.195.245:8000/wechat/wxLogin",

      data: {

      },
      header: {
        "Content-Type": "application/json"
      },
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        console.log("res=", res)
        console.log("刷卡函数res.data.data", res.data.data)

        that1.setData({
          MyCardList: res.data.data
        })
        // if()
        // console.log("卡片列表长度", that1.data.MyCardList.length)
        if (that1.data.MyCardList == null) {
          that1.setData({
            pickerArray: [],
            haveNocard: true,

          })
        } else {
          for (let i = 0; i < that1.data.MyCardList.length; i++) {
            var that2 = that1
            that1.setData({
              ['pickerArray[' + i + ']']: res.data.data[i].cardUid,
            })
          }
        }

        console.log("赋值后pickerArray=", that1.data.pickerArray)
      },
      fail: function (err) {}, //请求失败
      complete: function () {} //请求完成后执行的函数
    })
  },


  getCardChargeRecord: function () {
    var that1 = this
    wx.request({
      // url: 'http://8.136.195.245:5006/api/wxlogin/login1', //服务器
      url: ApiRootUrl+"charge/getCardChargeRecords",
      // url: "http://localhost:8001/charge/getCardChargeRecords",
      data: {
        cardUid: that1.data.selectCardUid
      },
      header: {
        "Content-Type": "application/json"
      },
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        console.log("卡片充值记录的res=", res)
        console.log("res.data.data=", res.data.data)
        that1.setData({
          CardChargeList: res.data.data,
          beforeSearch:false,
        })
      },
      fail: function (err) {}, //请求失败
      complete: function () {} //请求完成后执行的函数
    })
  },

  getCardOrderRecord: function () {
    var that1 = this
    wx.request({
      // url: 'http://8.136.195.245:5006/api/wxlogin/login1', //服务器
      url: ApiRootUrl+"orders/getOrderByCarduid",
      // url: "http://localhost:8001/orders/getOrderByCarduid",
      data: {
        cardUid: that1.data.selectCardUid
      },
      header: {
        "Content-Type": "application/json"
      },
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        console.log("卡片订单记录的res=", res)
        console.log("res.data.data=", res.data.data)
        that1.setData({
          CardOrderList: res.data.data,
          beforeSearch:false,
        })
      },
      fail: function (err) {}, //请求失败
      complete: function () {} //请求完成后执行的函数
    })
  },
})